package transformations;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import utils.TransformerTemplate;

import datensaetze.Datensatz;
import de.dnb.music.publicInterface.MusicRecord;
import de.dnb.music.publicInterface.TransformRecord;
import de.dnb.music.publicInterface.TransformRecord.TransformMode;
import exceptions.NoPreferredNameException;
import exceptions.NoSuchItemException;

public class MusicTransformer extends TransformerTemplate {

	public MusicTransformer() throws IOException {
		super();
		stream = new PrintStream("D:/Normdaten/log.txt");
	}

	private int unchanged = 0;

	private PrintStream stream;

	@Override
	protected String processData(final String rawData) {
		String processed = null;
		try {
			processed =
				TransformRecord.transform(rawData, TransformMode.MACHINE);
		} catch (Exception e) {
			System.err.println(rawData);
		}
		String rejCause = TransformRecord.getRejectionCause();
		if (!"".equals(rejCause)) {
			stream.println("-----------");
			stream.println(rejCause);
			stream.println();
			stream.println(rawData);
		}
		return processed;
	}

	@Override
	protected boolean isIdentical(String rawData, String newData) {
		return MusicRecord.isNormIdentical(rawData, newData);
	}

	@Override
	protected void log(String rawData) {
		//		super.log(rawData);
		unchanged++;
	}

	@Override
	public boolean finalAccept(String newData, String oldData) {

		Pattern umPattern =
			Pattern.compile("^130 .*um \\d\\d\\d\\d\\$", Pattern.MULTILINE);
		Matcher matcher = umPattern.matcher(oldData);
		if (matcher.find())
			return false;

		Pattern zahlZahlPattern =
			Pattern.compile("^130 .* \\d\\d\\d\\d\\ \\d\\$",
					Pattern.MULTILINE);
		matcher = zahlZahlPattern.matcher(oldData);
		if (matcher.find())
			return false;

		Pattern sechsAPattern =
			Pattern.compile("^130 .*, \\d[abcd]\\$", Pattern.MULTILINE);
		matcher = sechsAPattern.matcher(newData);
		if (matcher.find())
			return false;

		Pattern fuessePat =
			Pattern.compile("^130 .*Org 4 Füße", Pattern.MULTILINE);
		matcher = fuessePat.matcher(oldData);
		if (matcher.find())
			return false;

		Pattern fassungPat =
			Pattern.compile("^[14]30 .*\\.\\.\\. .*assung", Pattern.MULTILINE);
		matcher = fassungPat.matcher(oldData);
		if (matcher.find())
			return false;

		Pattern opPat =
			Pattern.compile("^130 [^$]* op\\.", Pattern.MULTILINE);
		matcher = opPat.matcher(newData);
		if (matcher.find())
			return false;

		Pattern minusPat =
			Pattern.compile("^130 [^$]* \\d+ - \\d+", Pattern.MULTILINE);
		matcher = minusPat.matcher(oldData);
		if (matcher.find())
			return false;

		Pattern plusPat =
			Pattern.compile("^130 [^$]* \\d+ \\+ \\d+", Pattern.MULTILINE);
		matcher = plusPat.matcher(oldData);
		if (matcher.find())
			return false;

		Pattern durPat = Pattern.compile("^130 .*Dur ", Pattern.MULTILINE);
		matcher = durPat.matcher(oldData);
		if (matcher.find())
			return false;

		Pattern mollPat = Pattern.compile("^130 .*Moll ", Pattern.MULTILINE);
		matcher = mollPat.matcher(oldData);
		if (matcher.find())
			return false;

		return true;
	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		TransformRecord.setUnmodifiables(null);

		MusicTransformer transformer = new MusicTransformer();
		transformer.setInputFile("D:/Normdaten/dma-wim-einspielung.dwl");
		transformer.setOutputFile("D:/Normdaten/dma-wim-einspielung.txt");

		transformer.work();

		System.err.println(transformer.unchanged);

	}

}
